package com.location.footprint.activity

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.graphics.drawable.GradientDrawable
import android.view.animation.DecelerateInterpolator
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.location.footprint.R
import com.location.footprint.adapter.ThemeAdapter
import com.location.footprint.base.BaseAppAplication
import com.location.footprint.bean.ThemeBean
import com.location.footprint.eventbus.EventBusUtils
import com.location.footprint.eventbus.EventCode
import com.location.footprint.eventbus.EventMessage
import com.location.footprint.manager.DividerDecoration
import com.location.footprint.toolbar.QMUIStatusBarHelper
import com.location.footprint.utils.Utils
import kotlinx.android.synthetic.main.activity_theme.*
import java.util.*


class ThemeActivity : BaseActivity() {
    private val tlist = ArrayList<ThemeBean>()
    private val mAdapter = ThemeAdapter()
    override fun getLayoutId(): Int {
        return R.layout.activity_theme
    }

    override fun initView() {
        super.initView()
        initStatus()
        initItemData()
    }

    private fun initStatus() {
        QMUIStatusBarHelper.translucent(this)
        //设置状态栏黑色字体与图标
        QMUIStatusBarHelper.setStatusBarLightMode(this)
        val relativeParams = view_status.layoutParams as RelativeLayout.LayoutParams //取控件textView当前的布局参数
        relativeParams.height = relativeParams.height + Utils.getStatusHeight(this)
        view_status.layoutParams = relativeParams
        text_toolbar_title.setOnClickListener { finish() }

    }

    private fun initItemData() {
        tlist.add(ThemeBean(R.color.white, true, "默认白"))
        tlist.add(ThemeBean(R.color.green, false, "酷安绿"))
        tlist.add(ThemeBean(R.color.red, false, "姨妈红"))
        tlist.add(ThemeBean(R.color.pick, false, "哔哩粉"))
        tlist.add(ThemeBean(R.color.yellow, false, "伊藤橙"))
        tlist.add(ThemeBean(R.color.blue, false, "知乎蓝"))

        recyclerview.layoutManager = LinearLayoutManager(this)
        val itemDecoration = DividerDecoration(ContextCompat.getColor(this, R.color.divider_gray), 2, 0, 0)//color & height & paddingLeft & paddingRight
        itemDecoration.setDrawLastItem(true)//sometimes you don't want draw the divider for the last item,default is true.
        recyclerview.addItemDecoration(itemDecoration)
        recyclerview.adapter = mAdapter
        mAdapter.setNewData(tlist)
        mAdapter.setOnItemClickListener { adapter, view, position ->

            setThemeColor(position)

        }
    }

    private fun setThemeColor(position: Int) {

        BaseAppAplication.theme = tlist[position].color!!
        if (BaseAppAplication.theme == R.color.white) {
            BaseAppAplication.theme = R.color.black
        }
        EventBusUtils.post(EventMessage(EventCode.EVENT_THEME, BaseAppAplication.theme))
        for (i in tlist.indices) tlist[i].select = false
        tlist[position].select = true
        mAdapter.notifyDataSetChanged()
        val animatorSet = AnimatorSet()  //组合动画
        val scaleX = ObjectAnimator.ofFloat(text_circle, "scaleX", 0f, 60f)
        val scaleY = ObjectAnimator.ofFloat(text_circle, "scaleY", 0f, 20f)
        animatorSet.duration = 1000  //动画时间
        animatorSet.interpolator = DecelerateInterpolator()  //设置插值器
        animatorSet.play(scaleX).with(scaleY)  //同时执行
        animatorSet.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animator: Animator) {
                var text_circle_bg = text_circle.background as GradientDrawable
                text_circle_bg.setColor(ContextCompat.getColor(this@ThemeActivity, tlist[position].color!!))
                text_circle.background = text_circle_bg
            }

            override fun onAnimationEnd(animator: Animator) {

                view_status.setBackgroundColor(ContextCompat.getColor(this@ThemeActivity, tlist[position].color!!))

            }

            override fun onAnimationCancel(animator: Animator) {

            }

            override fun onAnimationRepeat(animator: Animator) {

            }
        })
        animatorSet.start()  //启动动画
        if (position == 0) {
            QMUIStatusBarHelper.setStatusBarLightMode(this@ThemeActivity)
            text_toolbar_title.setTextColor(ContextCompat.getColor(this@ThemeActivity, R.color.gray))
            val homepressed = ContextCompat.getDrawable(this@ThemeActivity, R.drawable.abc_ic_ab_back_material)
            homepressed!!.setTint(ContextCompat.getColor(this@ThemeActivity, R.color.gray))
            homepressed!!.setBounds(0, 0, homepressed.minimumWidth, homepressed.minimumHeight)
            text_toolbar_title.setCompoundDrawables(homepressed, null, null, null)

        } else {
            QMUIStatusBarHelper.setStatusBarDarkMode(this@ThemeActivity)
            text_toolbar_title.setTextColor(ContextCompat.getColor(this@ThemeActivity, R.color.white))
            val homepressed = ContextCompat.getDrawable(this@ThemeActivity, R.drawable.abc_ic_ab_back_material)
            homepressed!!.setTint(ContextCompat.getColor(this@ThemeActivity, R.color.white))
            homepressed!!.setBounds(0, 0, homepressed.minimumWidth, homepressed.minimumHeight)
            text_toolbar_title.setCompoundDrawables(homepressed, null, null, null)

        }
    }

    override fun onResume() {
        super.onResume()
        for (i in tlist.indices) {
            if (tlist[i].color == BaseAppAplication.theme) {
                setThemeColor(i)
                return
            }

        }
    }
}